package csel.model.skills;

import csel.model.Direction;
import csel.model.Tile;
import csel.model.entity.Character;
import csel.model.entity.Entity;
import csel.model.CharacterStats;

public class DetectTrap extends RangedActiveSkill
{
	private static final long serialVersionUID = 5L;
	Character c;
	public DetectTrap(Character owner)
	{
		super(owner);
		c = owner;
		setName("default");
		
		setDescription("attempt to detect it");
		setAllocatedPoints(1);
	}
	
	public void use(Direction d, Tile t)
	{
		double chanceOfSuccess = 1.0 - ((1.0)/(Math.pow(2, getPointsAllocated()/1.99)));
		
		c.setCharacterStat(CharacterStats.ManaUsed, (c.getCharacterStat(CharacterStats.ManaUsed)+4));
		double roll = Math.random();
		if(roll <= chanceOfSuccess && c.getCharacterStat(CharacterStats.CurrentMana) >= 4)
		{
			/* HACK 
			 * 
			 * This is kind of weird only the center is a tile direction off. So it does not detect around the
			 * entity. The center is one tile away in the direction given.
			 * 
			 * */
			if(t.getNeighbor(Direction.NORTH) != null && t.getNeighbor(Direction.NORTH).getItem() != null)
			{
				t.getNeighbor(Direction.NORTH).getItem().setIsVisible(true);
			}
			else if(t.getNeighbor(Direction.NORTHWEST) != null && t.getNeighbor(Direction.NORTHWEST).getItem() != null)
			{
				t.getNeighbor(Direction.NORTHWEST).getItem().setIsVisible(true);
			}
			else if(t.getNeighbor(Direction.WEST) != null && t.getNeighbor(Direction.WEST).getItem() != null)
			{
				t.getNeighbor(Direction.WEST).getItem().setIsVisible(true);
			}
			else if(t.getNeighbor(Direction.SOUTHWEST) != null && t.getNeighbor(Direction.SOUTHWEST).getItem() != null)
			{
				t.getNeighbor(Direction.SOUTHWEST).getItem().setIsVisible(true);
			}
			else if(t.getNeighbor(Direction.SOUTH) != null && t.getNeighbor(Direction.SOUTH).getItem() != null)
			{
				t.getNeighbor(Direction.SOUTH).getItem().setIsVisible(true);
			}
			else if(t.getNeighbor(Direction.SOUTHEAST) != null && t.getNeighbor(Direction.SOUTHEAST).getItem() != null)
			{
				t.getNeighbor(Direction.SOUTHEAST).getItem().setIsVisible(true);
			}
			else if(t.getNeighbor(Direction.EAST) != null && t.getNeighbor(Direction.EAST).getItem() != null)
			{
				t.getNeighbor(Direction.EAST).getItem().setIsVisible(true);
			}
			else if(t.getNeighbor(Direction.NORTHEAST) != null && t.getNeighbor(Direction.NORTHEAST).getItem() != null)
			{
				t.getNeighbor(Direction.NORTHEAST).getItem().setIsVisible(true);
			}
		}
		
	}
	public void use(Entity e)
	{
		//TODO
	}
}
